<?php
/**
 * Created by swagger_zxd.
 * User: zxd
 * Date: 2018/2/23
 * Time: 14:05
 */

namespace app\admin\controller;


use app\index\controller\Base;

class Roomset extends Base{
    /**
     * 房间配置
     */
    public function roomSet(){
        if ($this->request->method()=="POST") {
            $data=$this->request->param();
            $room=$data['room']?"'".$data['room']."'":'null';
            $state=$data['state']?$data['state']:'null';
            $winlose=$data['winLose']?$data['winLose']:'null';

            $records='';
            try{
                $pdo=odbcLink();
                $sqlstr="exec Bg_SelRoomList2 ".$room.",".$state.",".$winlose;
                $sql = iconv("utf-8", "gbk", $sqlstr);
                $sth=$pdo->query($sql);
                $i=1;
                do{
                    $rowset= $sth->fetchAll(\PDO::FETCH_NUM);
                    if ($rowset) {
                        switch ($i) {
                            case 1:
                                if(!empty($rowset)){
                                    foreach ($rowset as $k=>$value) {
                                        $records[$k]['kindId']=$rowset[$k][0]; //kindId
                                        $records[$k]['serverid']=$rowset[$k][1]; //serverid
                                        $records[$k]['roomname']=iconv("gbk","utf-8",$rowset[$k][2]);  //房间名
                                        $records[$k]['zsy']=$rowset[$k][3];    //总输赢
                                        $records[$k]['drsy']=$rowset[$k][4];  //当日输赢
                                        $records[$k]['xcph']=$rowset[$k][5];  //血池平衡
                                        $records[$k]['syxc']=$rowset[$k][6];  //当前剩余血池
                                        $records[$k]['state']=iconv("gbk","utf-8",$rowset[$k][7]);  //状态
                                        $records[$k]['tfl']=$rowset[$k][8];       //吐分率
                                        $records[$k]['time']=$rowset[$k][9];    //处理时间
                                        $records[$k]['xcfs']=$rowset[$k][10];    //血池分数
                                        $records[$k]['xcrate']=$rowset[$k][11]; //当前血池税率
                                    }
                                } else {
                                    $records='';
                                }
                                break;
                            case 2:
                                $xtdqxc=$rowset[0][0]; //系统当前血池
                                $zsy=$rowset[0][1];    //总输赢
                                $drsy=$rowset[0][2];   //当日输赢
                                $drcf=$rowset[0][3];   //当日吃分
                                $drtf=$rowset[0][4];   //当日吐分
                                $drtfl=$rowset[0][5];  //当日吐分率
                                $countInfo=['xtdqxc'=>$xtdqxc,'zsy'=>$zsy,'drsy'=>$drsy,'drcf'=>$drcf,'drtf'=>$drtf,'drtfl'=>$drtfl];
                                break;
                            case 3:
                                break;
                        }
                    }
//                    dump($i);
//                    dump($rowset);
                    $i++;
                }while($sth->nextRowset());

            }catch(\Exception $e){
                echo $e->getMessage();
            }
            $out=['records'=>$records,'countInfo'=>$countInfo];

            return json($out);

        } elseif ($this->request->method()=="GET") {

            return $this->fetch('roomSet');
        }
    }

    //血池控制列表
    public function xueChiKongZhiList(){
        if ($this->request->method()=="POST") {  //返回数据
           $data=$this->request->param();

           if (array_key_exists('game',$data)) {
               $kindId=$data['game']?$data['game']:'null';
           }else{
               $kindId='null';
           }

           if (array_key_exists('room',$data)) {
               $serverId=$data['room']?$data['room']:'null';
           }else{
               $serverId='null';
           }
           $state=$data['state']?$data['state']:'-1';
           $page=$data['page']?$data['page']:1;
           //调用房间控制类
           $RoomModel=new \app\admin\model\roomSet();
           return $RoomModel->loadXueChiKongZhi($kindId,$serverId,$state,$page);
        } elseif ($this->request->method()=="GET") { //展示页面
            $data=$this->request->param();
            $kindId=$data['kindId'];
            $serverId=$data['serverid'];
            return $this->fetch('xueChiKongZhiList',['kindId'=>$kindId,'serverId'=>$serverId]);
        }
    }

    //历史吞吐率列表
    public function historicalThroughput(){
        if ($this->request->method()=="POST") {
            $data=$this->request->param();

            if (array_key_exists('game',$data)) {
                $kindId=$data['game']?$data['game']:'null';
            }else{
                $kindId='null';
            }

            if (array_key_exists('room',$data)) {
                $serverId=$data['room']?$data['room']:'null';
            }else{
                $serverId='null';
            }

            $page=$data['page']?$data['page']:1;
            //调用房间控制类
            $RoomModel=new \app\admin\model\roomSet();
            return $RoomModel->loadHistoricalThroughPut($kindId,$serverId,$page);



        } elseif ($this->request->method()=="GET") {
            $data=$this->request->param();
            $kindId=$data['kindId'];
            $serverId=$data['serverid'];
            return $this->fetch('historicalThroughput',['kindId'=>$kindId,'serverId'=>$serverId]);
        }
    }
    //血池变化列表
    public function bloodPoolChanges(){
        if ($this->request->method()=="POST") {
            $data=$this->request->param();
            if (array_key_exists('serverId',$data)) {
                $serverId=$data['serverId']?$data['serverId']:'null';
            }else{
                $serverId='null';
            }

            if (array_key_exists('begin',$data)) {
                $begin=$data['begin']?$data['begin']:'null';
            }else{
                $begin='null';
            }

            if (array_key_exists('end',$data)) {
                $end=$data['end']?$data['end']:'null';
            }else{
                $end='null';
            }
            $RoomModel=new \app\admin\model\roomSet();
            return $RoomModel->loadRoomBloodChangeChartDatas($serverId,$begin,$end);

        } elseif ($this->request->method()=="GET") {
            $data=$this->request->param();
            $serverId=$data['serverid'];
            return $this->fetch('bloodPoolChanges',['serverId'=>$serverId]);
        }
    }
    //添加血池控制
    public function addRoomSet(){
        if ($this->request->method()=="POST") {
            $data=$this->request->param();
            //服务id
            if (array_key_exists('serverid',$data)) {
                $serverId=$data['serverid']?$data['serverid']:'null';
            }else{
                $serverId='null';
            }
            //设置时间
            if (array_key_exists('setTime',$data)) {
                $setTime=$data['setTime']?$data['setTime']:'null';
            }else{
                $setTime='null';
            }
            //分数
            if (array_key_exists('score',$data)) {
                $score=$data['score']?$data['score']:'null';
            }else{
                $score='null';
            }
            //ip
            $ip=getIPaddress();
            $RoomModel=new \app\admin\model\roomSet();
            return $RoomModel->addRoomSet($serverId,$setTime,$score,$ip);

        }

    }
    //修改血池税率
    public function changeBloodRate(){
        if ($this->request->method()=="POST") {
            $data=$this->request->param();
            if (array_key_exists('serverid',$data)) {
                $serverId=$data['serverid']?$data['serverid']:'null';
            }else{
                $serverId='null';
            }
            //血池率
            if (array_key_exists('xc_rate',$data)) {
                $xc_rate=$data['xc_rate']?$data['xc_rate']:'null';
            }else{
                $xc_rate='null';
            }
            $RoomSet=new \app\admin\model\roomSet();
            return $RoomSet->changeBloodRate($serverId,$xc_rate);
        }
    }

}